package com.shermanzhou.springbootflowable.demo.custom.service;

import com.shermanzhou.springbootflowable.demo.custom.mapper.CustomSQLMapper;
import org.flowable.engine.ManagementService;
import org.flowable.engine.common.impl.cmd.CustomSqlExecution;
import org.flowable.engine.impl.cmd.AbstractCustomSqlExecution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author zhouchaoqun02
 * @date 2020-06-14 19:44
 */
@Service
public class CustomCommentService {

    @Autowired
    private ManagementService managementService;

    public CustomCommentService() {
    }

    public ManagementService getManagementService() {
        return managementService;
    }

    public void setManagementService(ManagementService managementService) {
        this.managementService = managementService;
    }

    public List<Map<String, Object>> getExecutionsByProcInstId(String procInstId) {
        CustomSqlExecution<CustomSQLMapper, List<Map<String, Object>>> customSqlExecution = new AbstractCustomSqlExecution<CustomSQLMapper, List<Map<String, Object>>>(CustomSQLMapper.class) {
            @Override
            public List<Map<String, Object>> execute(CustomSQLMapper customMapper) {
                return customMapper.select("act_ru_execution", procInstId);
            }
        };
        return managementService.executeCustomSql(customSqlExecution);
    }

    public Integer insert(String table, List<Map<String, Object>> entites) {
        CustomSqlExecution<CustomSQLMapper, Integer> customSqlExecution = new AbstractCustomSqlExecution<CustomSQLMapper, Integer>(CustomSQLMapper.class) {
            @Override
            public Integer execute(CustomSQLMapper customMapper) {
                return entites.stream().mapToInt(entity -> customMapper.insert(table, entity)).sum();
            }
        };
        return managementService.executeCustomSql(customSqlExecution);
    }
}